<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" width="200" align="center"></el-table-column>
      <el-table-column prop="description" label="简介"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="80" align="center"></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button type="primary" circle size="medium" @click="getListItem(scope.row)">修改</el-button>
            <el-button type="danger" circle size="medium" @click="del(scope.row)">删除</el-button>
          </el-row>
          <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="相册id" :label-width="formLabelWidth">
                <el-input v-model="form.id" autocomplete="off" disabled></el-input>
              </el-form-item>
              <el-form-item label="相册名称" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="相册简介" :label-width="formLabelWidth">
                <el-input v-model="form.description" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="排序序号" :label-width="formLabelWidth">
                <el-input v-model="form.sort" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="submit" @click="submit">确 定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script> import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        description: '',
        sort: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    loadAlbumList(){
      let url = "http://localhost:9080/albums/select"
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if(responseBody.state == 20000){
          this.tableData = responseBody.data;
        }
      })
    },
    getListItem(album){
      this.dialogFormVisible = true;
      this.form = album;
    },
    submit() {
      let url = 'http://localhost:9080/albums/'+this.form.id+'/update'
      let formData = this.qs.stringify(this.form);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          this.loadAlbumList();
        } else if (responseBody.state == 40400 ){
          this.$alert(responseBody.stateMessage, '警告',{
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = true;
              this.loadAlbumList();
            }
          });
        } else {
          this.$alert(responseBody.stateMessage, '警告',{
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = true;
              this.loadAlbumList();
            }
          });
        }
      })
    },
    del(album) {
      this.$confirm('此操作将永久删除'+'id为'+album.id+'的数据, 是否确认?', 'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);
      }).catch(() => {
        this.$message.info("已取消");
      });
    },
    handleDelete(album) {
      let url = 'http://localhost:9080/albums/'+album.id+'/delete'
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if(responseBody.state == 20000){
          this.$message.success("删除成功！");
          this.loadAlbumList();
        }else if(responseBody.state==40400){
          this.$message.warning(responseBody.message);
        }else {
          this.$message.warning(responseBody.message);
        }
      })
    }
  },
  mounted() {
    console.log("mounted ...")

    this.loadAlbumList();
  }
} </script>
